package com.woniu.inlet.timer;

import com.woniu.adapter.DailyIncomeAdapter;
import com.woniu.adapter.PersonalIncomeAdapter;
import com.woniu.outlet.mysql.po.PersonalIncomeMySQL;
import com.woniu.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 个人收益定时任务
 */
@Component
@Slf4j
public class PersonalIncomeTimer {

    @Autowired
    PersonalIncomeAdapter personalIncomeAdapter;

    @Autowired
    private DailyIncomeAdapter dailyIncomeAdapter;

    /**
     * "0 0 1 ? * *" 每天1:00 发送
     * 修改等待收益的单子状态为开始收益状态
     */
    @Scheduled(cron ="0 0 1 ? * *")
    public void dailySend() {
        log.debug("【修改等待收益的单子状态为开始收益状态】定时任务开始，当前时间{}", DateUtil.getBuyTime());
        //得到今日日期
        String sendDate = DateUtil.getSendDate();
        //查询status=-1的收益单子，日期一致开始算收益
        List<PersonalIncomeMySQL> startIncome = personalIncomeAdapter.getStartIncome();
        int count=0;
        for (PersonalIncomeMySQL p : startIncome) {
             if(p.getStartIncomeDate().equals(sendDate)){
                 //修改状态
                 personalIncomeAdapter.updateStatus_1To0(p);
                 count++;
             }
        }
        log.debug("共有{}个单子需要开始算收益",count);
    }
}
